package com.mall.huohuo.openapi.config.auth;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro配置
 *
 * @author zycoding
 */
@Configuration
public class ShiroConfig {

    /**
     * shirofilter 配置
     *
     * @return shirofilter
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(this.securityManager());

        shiroFilterFactoryBean.setLoginUrl("/auth/login");

        shiroFilterFactoryBean.getFilters().put("jwt", new JwtFilter());

        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 注册、登录相关
//        filterChainDefinitionMap.put("/admin/login/mobile", "anon");
        filterChainDefinitionMap.put("/auth/login1", "anon");
        filterChainDefinitionMap.put("/auth/login", "anon");
        filterChainDefinitionMap.put("/home/**", "anon");
        filterChainDefinitionMap.put("/advertise/**", "anon");
//        filterChainDefinitionMap.put("/admin/login/account", "anon");
//        filterChainDefinitionMap.put("/admin/login/mobileVerifyCode", "anon");
//        filterChainDefinitionMap.put("/api/wxMobile/login", "anon");
//
//        filterChainDefinitionMap.put("/api/login/mobile", "anon");
//        filterChainDefinitionMap.put("/api/login/mobileVerifyCode", "anon");
//        filterChainDefinitionMap.put("/api/login", "anon");
//
//        filterChainDefinitionMap.put("/api/alipay/miniapp/login", "anon");
//        filterChainDefinitionMap.put("/api/tt/miniapp/login", "anon");
//
//        filterChainDefinitionMap.put("/api/register/mobileVerifyCode", "anon");
//        filterChainDefinitionMap.put("/api/wxMp/getWxMpLoginConfig", "anon");
//        filterChainDefinitionMap.put("/api/wxMp/login", "anon");
//        filterChainDefinitionMap.put("/api/v1/platform/login", "anon");
//        filterChainDefinitionMap.put("/api/v1/platform/getToken", "anon");
//
//        // 支付回调
//        filterChainDefinitionMap.put("/api/pay/callback/weChat/v1", "anon");
//        filterChainDefinitionMap.put("/api/pay/callback/alipay/v1", "anon");
//        filterChainDefinitionMap.put("/api/telephone/callback/back", "anon");
//
//        filterChainDefinitionMap.put("/api/orderStatistics/**", "anon");
//        filterChainDefinitionMap.put("/api/pay/alipay/createWebPayTrade", "anon");
//
//        // 其他
//        filterChainDefinitionMap.put("/api/city/**", "anon");
//        filterChainDefinitionMap.put("/api/v2/cities/**", "anon");
//        filterChainDefinitionMap.put("/api/v2/regions/**", "anon");
//        filterChainDefinitionMap.put("/api/v2/films/**", "anon");
//        filterChainDefinitionMap.put("/api/v2/shows/**", "anon");
//        filterChainDefinitionMap.put("/api/movie/**", "anon");
//        filterChainDefinitionMap.put("/api/wxapi/**", "anon");
//        filterChainDefinitionMap.put("/api/pay/back/**", "anon");
//        filterChainDefinitionMap.put("/api/callback/**", "anon");
//        filterChainDefinitionMap.put("/admin/login/**", "anon");
//        filterChainDefinitionMap.put("/api/register/mobile", "anon");
//        filterChainDefinitionMap.put("/api/oauth/refresh", "anon");
//        filterChainDefinitionMap.put("/api/sms/sendVerifyCode", "anon");
//        filterChainDefinitionMap.put("/api/ws/supplier", "anon");
//        filterChainDefinitionMap.put("/ws/message", "anon");
//
//        filterChainDefinitionMap.put("/api/kfcStore/tip", "anon");
//        filterChainDefinitionMap.put("/api/advertisement/*", "anon");
//        filterChainDefinitionMap.put("/api/config/getAll", "anon");
//
//        filterChainDefinitionMap.put("/api/v1/wx/mp/auth/**", "anon");
//        filterChainDefinitionMap.put("/api/v1/wx/mp/message/**", "anon");
//        filterChainDefinitionMap.put("/api/v1/wx/mp/checkSignature", "anon");
//        filterChainDefinitionMap.put("/api/v1/wx/mp/createJsapiSignature", "anon");
//
//        filterChainDefinitionMap.put("/assets/**", "anon");
//        filterChainDefinitionMap.put("/MP_verify_PM814PReoVTmeAuv.txt", "anon");
//        filterChainDefinitionMap.put("/fq2qRirXZ5.txt", "anon");
//        filterChainDefinitionMap.put("/api/v1/platform/movieOrder/**", "anon");
//        filterChainDefinitionMap.put("/api/v1/platform/**", "anon");
//        filterChainDefinitionMap.put("/api/v2/platform/**", "anon");
//        filterChainDefinitionMap.put("/api/v3/platform/**", "anon");
//        filterChainDefinitionMap.put("/api/v1/platform/redirectToApp", "anon");
//
//        // swagger
//        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
//        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
//        filterChainDefinitionMap.put("/swagger/**", "anon");
//        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
//        filterChainDefinitionMap.put("/**/v2/api-docs", "anon");
//        filterChainDefinitionMap.put("/**/*.js", "anon");
//        filterChainDefinitionMap.put("/**/*.css", "anon");
//        filterChainDefinitionMap.put("/**/*.png", "anon");
//        filterChainDefinitionMap.put("/**/*.ico", "anon");
//
//        // 开放接口
//        filterChainDefinitionMap.put("/openApi/**", "anon");

        // filterChainDefinitionMap.put("/admin/**", "roles[admin]");

        filterChainDefinitionMap.put("/**", "jwt");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(this.realm());
        securityManager.setCacheManager(this.cacheManager());
        securityManager.setSessionManager(this.sessionManager());

        return securityManager;
    }

    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionIdCookieEnabled(false);
        sessionManager.setSessionIdUrlRewritingEnabled(false);

        return sessionManager;
    }

    @Bean
    public Realm realm() {
        return new JwtRealm();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
            @Qualifier("securityManager") SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public CacheManager cacheManager() {
        CacheManager cacheManager = new EhCacheManager();
        return cacheManager;
    }
}
